package org.example.demo_ottery.common.interceptor;


import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.demo_ottery.common.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    private final static Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);

    /**
     * 预处理，请求之前调用
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request
            , HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("user_token");
        logger.info("请求token:{}",token);
        logger.info("请求路径:{}",request.getPathInfo());
        //解析token
        Claims claims = JWTUtil.parseJWT(token);
        if(null==claims) {
            logger.error("解析失败！");
            return false;
        }
        logger.info("解析成功");
        return true;
    }
}
